package com.hzy.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.oms.domain.bo.AftersaleItemBo;
import com.hzy.oms.domain.vo.AftersaleItemVo;
import com.hzy.oms.domain.AftersaleItem;
import com.hzy.oms.mapper.AftersaleItemMapper;
import com.hzy.oms.service.IAftersaleItemService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单售后Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class AftersaleItemServiceImpl implements IAftersaleItemService {

    private final AftersaleItemMapper baseMapper;

    /**
     * 查询订单售后
     */
    @Override
    public AftersaleItemVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单售后列表
     */
    @Override
    public TableDataInfo<AftersaleItemVo> queryPageList(AftersaleItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AftersaleItem> lqw = buildQueryWrapper(bo);
        Page<AftersaleItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单售后列表
     */
    @Override
    public List<AftersaleItemVo> queryList(AftersaleItemBo bo) {
        LambdaQueryWrapper<AftersaleItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AftersaleItem> buildQueryWrapper(AftersaleItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AftersaleItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMemberId() != null, AftersaleItem::getMemberId, bo.getMemberId());
        lqw.eq(bo.getAftersaleId() != null, AftersaleItem::getAftersaleId, bo.getAftersaleId());
        lqw.eq(bo.getOrderId() != null, AftersaleItem::getOrderId, bo.getOrderId());
        lqw.eq(bo.getOrderItemId() != null, AftersaleItem::getOrderItemId, bo.getOrderItemId());
        lqw.eq(bo.getReturnAmount() != null, AftersaleItem::getReturnAmount, bo.getReturnAmount());
        lqw.eq(bo.getQuantity() != null, AftersaleItem::getQuantity, bo.getQuantity());
        return lqw;
    }

    /**
     * 新增订单售后
     */
    @Override
    public Boolean insertByBo(AftersaleItemBo bo) {
        AftersaleItem add = BeanUtil.toBean(bo, AftersaleItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单售后
     */
    @Override
    public Boolean updateByBo(AftersaleItemBo bo) {
        AftersaleItem update = BeanUtil.toBean(bo, AftersaleItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AftersaleItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单售后
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
